package com.firefly.service.command.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.cms.po.LinkTypePo;
import com.firefly.dao.dao.cms.po.LinkPo;
import com.firefly.dao.dao.cms.service.LinkService;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.assembler.cms.LinkTypeAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.cms.LinkTypeDto;
import com.firefly.service.repository.cms.LinkTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 链接分类
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/20
 **/
@Service
public class LinkTypeCommand {
    private final LinkTypeAssembler linkTypeAssembler;
    private final LinkTypeRepository linkTypeRepository;
    private final LinkService linkService;
    @Autowired
    public LinkTypeCommand(LinkTypeAssembler linkTypeAssembler, LinkTypeRepository linkTypeRepository, LinkService linkService){
        this.linkTypeAssembler = linkTypeAssembler;
        this.linkTypeRepository = linkTypeRepository;
        this.linkService = linkService;
    }
    public Boolean create(LinkTypeDto request){
        LinkTypePo entity = linkTypeAssembler.getPo(request);
        return linkTypeRepository.create(entity);
    }
    public Boolean update(LinkTypeDto request){
        LinkTypePo entity = linkTypeAssembler.getPo(request);
        return linkTypeRepository.update(entity);
    }
    public Boolean remove(CommonDto request){
        QueryWrapper<LinkPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("linktype_id",request.getDetailIds()).eq("del_flag",0);
        if (linkService.count(queryWrapper) > 0){
            throw new FireflyException(403,"请先删除分类下链接");
        }
        return linkTypeRepository.updateDelFlagByIds(request.getDetailIds());
    }
}
